package com.wits.qso.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.HWKAttachmentDaoCustom;
import com.wits.qso.entity.CourseRegister;
import com.wits.qso.entity.HWKAttachment;

/**
 * 作业附件数据操作实现
 */
@Component
public class HWKAttachmentDaoImpl extends BaseDaoImpl<HWKAttachment, String>
		implements HWKAttachmentDaoCustom {

	@SuppressWarnings("unchecked")
	public List<HWKAttachment> findHWKAttachments(Map<String, Object> param) {
		List<HWKAttachment> list=new ArrayList<HWKAttachment>();
		if(param==null){
			return list;
		}
		StringBuffer buf = new StringBuffer();
		buf.append(" SELECT HWKF_ID,HWK_ID,FILE_OLD_NAME,FILE_NEW_NAME,FILE_PATH,FILE_URI,");
		buf.append(" FILE_SIZE,FILE_DESC,REMARK, USER_CRE, DATE_CRE, USER_UPD, DATE_UPD ");
		buf.append(" FROM QSO_HOMEWORK_F WHERE 1=1 ");
		if (param.get("HWKF_ID") != null) {
			buf.append(" and HWKF_ID=:HWKF_ID ");
		}
		if (param.get("HWK_ID") != null) {
			buf.append(" and HWK.HWK_ID=:HWK_ID ");
		}
		if (param.get("FILE_OLD_NAME") != null) {
			buf.append(" and FILE_OLD_NAME like %:FILE_OLD_NAME% ");
		}
		if (param.get("FILE_NEW_NAME") != null) {
			buf.append(" and FILE_NEW_NAME like %:FILE_NEW_NAME% ");
		}
		if (param.get("FILE_PATH") != null) {
			buf.append(" and FILE_PATH =:FILE_PATH ");
		}
		if (param.get("FILE_URI") != null) {
			buf.append(" and FILE_URI =:FILE_URI ");
		}
		if (param.get("FILE_SIZE") != null) {
			buf.append(" and FILE_SIZE =:FILE_SIZE ");
		}
		if (param.get("FILE_DESC") != null) {
			buf.append(" and FILE_DESC like %:FILE_DESC% ");
		}

		Query a = em.createNativeQuery(buf.toString(), CourseRegister.class);
		if (param.get("HWKF_ID") != null) {
			a.setParameter("HWKF_ID", param.get("HWKF_ID"));
		}
		if (param.get("HWK_ID") != null) {
			a.setParameter("HWK_ID", param.get("HWK_ID"));
		}
		if (param.get("FILE_OLD_NAME") != null) {
			a.setParameter("FILE_OLD_NAME", param.get("FILE_OLD_NAME"));
		}
		if (param.get("FILE_NEW_NAME") != null) {
			a.setParameter("FILE_NEW_NAME", param.get("FILE_NEW_NAME"));
		}
		if (param.get("FILE_PATH") != null) {
			a.setParameter("FILE_PATH", param.get("FILE_PATH"));
		}
		if (param.get("FILE_URI") != null) {
			a.setParameter("FILE_URI", param.get("FILE_URI"));
		}
		if (param.get("FILE_SIZE") != null) {
			a.setParameter("FILE_SIZE", param.get("FILE_SIZE"));
		}
		if (param.get("FILE_DESC") != null) {
			a.setParameter("FILE_DESC", param.get("FILE_DESC"));
		}
		
		list = a.getResultList();
		return list;
	}

	public boolean modifyFeild(Map<String, Object> param) {
		boolean res=false;
		try {
			String sql = " update qso_homework_h set FILE_PATH=:FILE_PATH where hwkh_id=:hwkh_id ";
			Query query = em.createNativeQuery(sql);
			query.setParameter("hwkh_id", param.get("hwkh_id"));
			query.setParameter("file_path", param.get("file_path"));

			query.executeUpdate();
			res=true;
		} catch (Exception e) {
			e.printStackTrace();
			res=false;
		}
		return res;
	}

	

	/**
	 * @param args
	 */
	public static void main(String[] args){

	}
}
